package view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * This class is the Menu that appears when the user wishes to change
 * the controls for the game.
 * 
 * @author Stephanie
 *
 */

public class ControllerConfigMenu extends JPanel implements PropertyChangeListener, KeyListener{
	
	public static final String ID = "CONTROLLER_CONFIG_MENU";
	
	private MainFrame mainFrame;
	
	private JButton acceptButton;
	private JButton cancelButton;
	private JButton resetButton;
	
	private JTextField[] fields;
	private String[] values;
	
	private JLabel northwestLabel;
    private JLabel northLabel;
    private JLabel northeastLabel;
    private JLabel southwestLabel;
    private JLabel southLabel;
    private JLabel southeastLabel;
    private JLabel useDefaultAttackLabel;
    private JLabel useUtilityAbilityLabel;
    private JLabel nextUtilityAbilityLabel;
    private JLabel prevUtilityAbilityLabel;
    private JLabel scrollNorthLabel;
    private JLabel scrollNortheastLabel;
    private JLabel scrollNorthwestLabel;;
    private JLabel scrollSouthLabel;;
    private JLabel scrollSoutheastLabel;
    private JLabel scrollSouthwestLabel;
    private JLabel dismountLabel;
    
    private static String northwestString = "Move Northwest";
    private static String northString = "Move North";
    private static String northeastString = "Move Northeast";
    private static String southwestString = "Move Southwest";
    private static String southString = "Move South";
    private static String southeastString = "Move Southeast";
    private static String useDefaultAttackString = "Use Default Attack";
    private static String useUtilityAbilityString = "Use Utility Ability";
    private static String nextUtilityAbilityString = "Next Utility Ability";
    private static String prevUtilityAbilityString = "Previous Utility Ability";
    private static String scrollNorthString = "Scroll North";
    private static String scrollNortheastString = "Scroll Northeast";
    private static String scrollNorthwestString = "Scroll Northwest";
    private static String scrollSouthString = "Scroll South";
    private static String scrollSouthwestString = "Scroll Southwest";
    private static String scrollSoutheastString = "Scroll Southeast";
    private static String dismountString = "Dismount";
    
    private JTextField northwestField;
	private JTextField northField;
	private JTextField northeastField;
	private JTextField southwestField;
	private JTextField southField;
	private JTextField southeastField;
	private JTextField useDefaultAttackField;
	private JTextField useUtilityAbilityField;
	private JTextField nextUtilityAbilityField;
	private JTextField prevUtilityAbilityField;
	private JTextField scrollNorthField;
	private JTextField scrollNortheastField;
	private JTextField scrollNorthwestField;
	private JTextField scrollSouthField;
	private JTextField scrollSouthwestField;
	private JTextField scrollSoutheastField;
	private JTextField dismountField;
	
	
	public ControllerConfigMenu( MainFrame mainFrame ) {
		this.mainFrame = mainFrame;
		
		this.setLayout(new BorderLayout());
		
		values = new String[17];
		setDefaultValues();
		
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new GridLayout(1,3));
		
		acceptButton = new JButton("Accept");
		acceptButton.setSize(150,30);
		acceptButton.setLocation(125,10);
		acceptButton.setVisible(true);
		buttonPane.add(acceptButton);
		
		cancelButton = new JButton("Cancel");
		cancelButton.setSize(150,30);
		cancelButton.setLocation(325,10);
		cancelButton.setVisible(true);
		buttonPane.add(cancelButton);
		
		resetButton = new JButton("Reset");
		resetButton.setVisible(true);
		resetButton.setSize(150,30);
		resetButton.setLocation(525,10);
		buttonPane.add(resetButton);
		
        northwestLabel = new JLabel(northwestString + "  ", JLabel.RIGHT);
        northLabel = new JLabel(northString + "  ", JLabel.RIGHT);
        northeastLabel = new JLabel(northeastString + "  ", JLabel.RIGHT);
        southwestLabel = new JLabel(southwestString + "  ", JLabel.RIGHT);
        southLabel = new JLabel(southString + "  ", JLabel.RIGHT);
        southeastLabel = new JLabel(southeastString + "  ", JLabel.RIGHT);
        useDefaultAttackLabel = new JLabel(useDefaultAttackString + "   ", JLabel.RIGHT);
        useUtilityAbilityLabel = new JLabel(useUtilityAbilityString + "  ", JLabel.RIGHT);
        nextUtilityAbilityLabel = new JLabel(nextUtilityAbilityString + "  ", JLabel.RIGHT);
        prevUtilityAbilityLabel = new JLabel(prevUtilityAbilityString + "  ", JLabel.RIGHT);
        scrollNorthLabel = new JLabel(scrollNorthString + "  ", JLabel.RIGHT);
        scrollNortheastLabel = new JLabel(scrollNortheastString + "  ", JLabel.RIGHT);
        scrollNorthwestLabel = new JLabel(scrollNorthwestString + "  ", JLabel.RIGHT);
        scrollSouthLabel = new JLabel(scrollSouthString + "  ", JLabel.RIGHT);
        scrollSoutheastLabel = new JLabel(scrollSoutheastString + "  ", JLabel.RIGHT);
        scrollSouthwestLabel = new JLabel(scrollSouthwestString + "  ", JLabel.RIGHT);
        dismountLabel = new JLabel(dismountString + "  ", JLabel.RIGHT);
		
      //Create the text fields and set them up.
        northwestField = new JTextField();
        northwestField.setText(values[5]);
        northwestField.setColumns(20);
        northwestField.addPropertyChangeListener("value", this);
        northwestField.addKeyListener(this);

        northField = new JTextField();
        northField.setText(values[0]);
        northField.addPropertyChangeListener("value", this);
        northField.addKeyListener(this);

        northeastField = new JTextField();
        northeastField.setText(values[1]);
        northeastField.addPropertyChangeListener("value", this);
        northeastField.addKeyListener(this);
        
        southwestField = new JTextField();
        southwestField.setText(values[4]);
        southwestField.addKeyListener(this);
        southwestField.addPropertyChangeListener("value", this);
        
        southField = new JTextField();
        southField.setText(values[3]);
        southField.addKeyListener(this);
        southField.addPropertyChangeListener("value", this);
        
        southeastField = new JTextField();
        southeastField.setText(values[2]);
        southeastField.addKeyListener(this);
        southeastField.addPropertyChangeListener("value", this);
        
        useDefaultAttackField = new JTextField();
        useDefaultAttackField.setText(values[6]);
        useDefaultAttackField.addKeyListener(this);
        useDefaultAttackField.addPropertyChangeListener("value",this);
        
        useUtilityAbilityField = new JTextField();
        useUtilityAbilityField.setText(values[7]);
        useUtilityAbilityField.addKeyListener(this);
        useUtilityAbilityField.addPropertyChangeListener("value",this);
        
        nextUtilityAbilityField = new JTextField();
        nextUtilityAbilityField.setText(values[8]);
        nextUtilityAbilityField.addKeyListener(this);
        nextUtilityAbilityField.addPropertyChangeListener("value",this);
        
        prevUtilityAbilityField = new JTextField();
        prevUtilityAbilityField.setText(values[9]);
        prevUtilityAbilityField.addKeyListener(this);
        prevUtilityAbilityField.addPropertyChangeListener("value",this);
        
        scrollNorthField = new JTextField();
        scrollNorthField.setText(values[10]);
        scrollNorthField.addKeyListener(this);
        scrollNorthField.addPropertyChangeListener("value",this);
        
        scrollNortheastField = new JTextField();
        scrollNortheastField.setText(values[11]);
        scrollNortheastField.addKeyListener(this);
        scrollNortheastField.addPropertyChangeListener("value",this);
        
        scrollNorthwestField = new JTextField();
        scrollNorthwestField.setText(values[15]);
        scrollNorthwestField.addKeyListener(this);
        scrollNorthwestField.addPropertyChangeListener("value",this);
        
        scrollSouthField = new JTextField();
        scrollSouthField.setText(values[13]);
        scrollSouthField.addKeyListener(this);
        scrollSouthField.addPropertyChangeListener("value",this);
        
        scrollSouthwestField = new JTextField();
        scrollSouthwestField.setText(values[14]);
        scrollSouthwestField.addKeyListener(this);
        scrollSouthwestField.addPropertyChangeListener("value",this);
        
        scrollSoutheastField = new JTextField();
        scrollSoutheastField.setText(values[12]);
        scrollSoutheastField.addKeyListener(this);
        scrollSoutheastField.addPropertyChangeListener("value",this);
        
        dismountField = new JTextField();
        dismountField.setText(values[16]);
        dismountField.addKeyListener(this);
        dismountField.addPropertyChangeListener("value",this);
        
        //Tell accessibility tools about label/textfield pairs.
        northwestLabel.setLabelFor(northwestField);
        northLabel.setLabelFor(northField);
        northeastLabel.setLabelFor(northeastField);
        southwestLabel.setLabelFor(southwestField);
        southLabel.setLabelFor(southField);
        southeastLabel.setLabelFor(southeastField);
        useDefaultAttackLabel.setLabelFor(useDefaultAttackField);
        useUtilityAbilityLabel.setLabelFor(useUtilityAbilityField);
        nextUtilityAbilityLabel.setLabelFor(nextUtilityAbilityField);
        prevUtilityAbilityLabel.setLabelFor(prevUtilityAbilityField);
        scrollNorthwestLabel.setLabelFor(scrollNorthwestField);
        scrollNorthLabel.setLabelFor(scrollNorthField);
        scrollNortheastLabel.setLabelFor(scrollNortheastField);
        scrollSouthwestLabel.setLabelFor(scrollSouthwestField);
        scrollSouthLabel.setLabelFor(scrollSouthField);
        scrollSoutheastLabel.setLabelFor(scrollSoutheastField);
        dismountLabel.setLabelFor(dismountField);
        
        
        //Lay out the labels in a panel.        
        
        JPanel labelPane = new JPanel(new GridLayout(0,1));
        labelPane.add(northwestLabel);
        labelPane.add(northLabel);
        labelPane.add(northeastLabel);
        labelPane.add(southwestLabel);
        labelPane.add(southLabel);
        labelPane.add(southeastLabel);
        labelPane.add(useDefaultAttackLabel);
        labelPane.add(useUtilityAbilityLabel);
        labelPane.add(nextUtilityAbilityLabel);
        labelPane.add(prevUtilityAbilityLabel);
        labelPane.add(scrollNorthwestLabel);
        labelPane.add(scrollNorthLabel);
        labelPane.add(scrollNortheastLabel);
        labelPane.add(scrollSouthwestLabel);
        labelPane.add(scrollSouthLabel);
        labelPane.add(scrollSoutheastLabel);
        labelPane.add(dismountLabel);

        //Layout the text fields in a panel.
        JPanel fieldPane = new JPanel(new GridLayout(0,1));
        fieldPane.add(northwestField);
        fieldPane.add(northField);
        fieldPane.add(northeastField);
        fieldPane.add(southwestField);
        fieldPane.add(southField);
        fieldPane.add(southeastField);
        fieldPane.add(useDefaultAttackField);
        fieldPane.add(useUtilityAbilityField);
        fieldPane.add(nextUtilityAbilityField);
        fieldPane.add(prevUtilityAbilityField);
        fieldPane.add(scrollNorthwestField);
        fieldPane.add(scrollNorthField);
        fieldPane.add(scrollNortheastField);
        fieldPane.add(scrollSouthwestField);
        fieldPane.add(scrollSouthField);
        fieldPane.add(scrollSoutheastField);
        fieldPane.add(dismountField);

        //Put the panels in this panel, labels on left,
        //text fields on right.
        labelPane.setVisible(true);
        fieldPane.setVisible(true);
        
        JPanel completePane = new JPanel(new GridLayout(1,2));
        completePane.add(labelPane);
        completePane.add(fieldPane);
        completePane.setVisible(true);
        
        this.add(buttonPane, BorderLayout.SOUTH);
        this.add(labelPane, BorderLayout.CENTER);
        this.add(fieldPane, BorderLayout.EAST);
        
        /*
        for(int k=0; k<fields.length; k++)
        {
        	if(fields[k] == null)
        		continue;
        	fields[k].setText("");
        }
        */
	}
	
	public void setDefaultValues(){
        values[0] = "8";
        values[1] = "9";
        values[2] = "3";
        values[3] = "2";
        values[4] = "1";
        values[5] = "7";
        values[6] = " ";
        values[7] = "u";
        values[8] = "";
        values[9] = "";
        values[10] = "w";
        values[11] = "e";
        values[12] = "c";
        values[13] = "x";
        values[14] = "z";
        values[15] = "q";
        values[16] = "/";
        
        
        /*     
         * 	private static final int MOVE_NORTH = 0;
    	 *	private static final int MOVE_NORTHEAST = 1;
    	 *	private static final int MOVE_SOUTHEAST = 2;
    	 *	private static final int MOVE_SOUTH = 3;
    	 * 	private static final int MOVE_SOUTHWEST = 4;
    	 * 	private static final int MOVE_NORTHWEST = 5;
		 *  private static final int USE_DEFAULT_ATTACK = 6;
		 *  private static final int USE_UTILITY_ABILITY = 7;
		 *  private static final int NEXT_UTILITY_ABILITY = 8;
		 *  private static final int PREV_UTILITY_ABILITY = 9;
		 *  private static final int SCROLL_NORTH = 10;
		 *  private static final int SCROLL_NORTHEAST = 11;
		 *  private static final int SCROLL_SOUTHEAST = 12;
		 *  private static final int SCROLL_SOUTH = 13;
		 *  private static final int SCROLL_SOUTHWEST = 14;
		 *  private static final int SCROLL_NORTHWEST = 15;
		 *  private static final int DISMOUNT = 16;
         */
	}
	
	public void addAcceptListener( ActionListener listener ) {
		acceptButton.addActionListener( listener );
	}

	public void addCancelListener( ActionListener listener ) {
		cancelButton.addActionListener( listener );
	}
	
	public void addResetListener( ActionListener listener ) {
		resetButton.addActionListener( listener );
	}

	public void propertyChange(PropertyChangeEvent e) {
		JFormattedTextField source = (JFormattedTextField) e.getSource();
		
		for ( int i = 0; i < 10; i++ ) {
			if ( i == 5 )
				i++;
			if ( source == fields[i] ) {
				values[i] = ((String) fields[i].getText());			
				fields[i].setText(values[i]);
				//checkIfExists(source,values[i]);
			}
		}
    }

	@Override
	public void keyPressed(KeyEvent e) {
		JTextField source = (JTextField) e.getSource();
		source.setText("");
		e.consume();
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
